<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* BASKET class
*
* Management of resource baskets
*
* $Header: /cvsroot/wikindx/wikindx3/core/basket/BASKET.php,v 1.4 2007/12/14 17:41:28 sirfragalot Exp $
*****/
class BASKET
{
// Constructor
	function BASKET($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/html/CLOSE.php");
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
	}
// Add resource to basket
	function add()
	{
		if($this->session->issetVar('basket_list'))
			$basket = unserialize($this->session->getVar('basket_list'));
		else
			$basket = array();
		$resourceId = $this->vars['resourceId'];
		if(array_search($resourceId, $basket) === FALSE)
			$basket[] = $resourceId;
// Ensure array is unique
		array_unique($basket);
		$this->session->setVar('basket_list', serialize($basket));
// send back to view this resource with success message
		include_once("core/resource/RESOURCEVIEW.php");
		$resource = new RESOURCEVIEW($this->db, $this->vars);
		new CLOSE($this->db, $resource->view($this->success->text("basketAdd"), 'lastSolo'));
	}
// Remove resource from basket
	function remove()
	{
		if($this->session->issetVar('basket_list'))
			$basket = unserialize($this->session->getVar('basket_list'));
		else
			$basket = array();
		$resourceId = $this->vars['resourceId'];
		if(($key = array_search($resourceId, $basket)) !== FALSE)
			unset($basket[$key]);
		$this->session->setVar('basket_list', serialize($basket));
// send back to view this resource with success message
		include_once("core/resource/RESOURCEVIEW.php");
		$resource = new RESOURCEVIEW($this->db, $this->vars);
		new CLOSE($this->db, $resource->view($this->success->text("basketRemove"), 'lastSolo'));
	}
// View the basket
	function view($message = FALSE)
	{
		include_once("core/template/TEMPLATE.php");
		$template = new TEMPLATE('content');
		include_once("core/list/LISTCOMMON.php");
		$common = new LISTCOMMON($this->db, $this->vars);
		$template->setVar('heading', $this->messages->text("heading", "basket"));
/*
		if(array_key_exists('bookmark', $this->vars))
		{
			$bookmarks = $this->session->getArray("bookmark");
			$sqlTotal = stripslashes(base64_decode($this->session->getVar("bookmark_lastMultiSql")));
			$queryString = "action=basketView&bookmark=1";
			list($pString, $pagingString) = $common->listResources($sqlTotal, $queryString, 'basket', 
				FALSE, $message);
			$template->setVar('body', $pString);
			$template->setVar('paging', $pagingString);
			return $template->process();
		}
*/
		$basket = unserialize($this->session->getVar('basket_list'));
		$condition = " WHERE ";
		foreach($basket as $id)
			$conditionArray[] = $this->db->tidyInput($id);
		$condition .= " (WKX_resource.id = " . implode(" OR WKX_resource.id = ", $conditionArray) . ")";
		include_once("core/sql/STATEMENTS.php");
		$stmt = new STATEMENTS($this->db);
		$sqlTotal = $this->db->selectNoExecute(array("WKX_resource"),  
			$stmt->listFields('basket'), $stmt->listJoin('basket', $condition));
		list($sqlTotal, $pagingObject) = $common->pagingStyle($sqlTotal, 'creator', TRUE);
		$queryString = "action=basketView";
		list($pString, $pagingString) = $common->listResources($sqlTotal, $queryString, 
			'basket', FALSE, $message, $pagingObject);
		$template->setVar('body', $pString);
		$template->setVar('paging', $pagingString);
		return $template->process();
	}
// Delete the basket
	function delete($confirm = FALSE)
	{
		if($confirm)
		{
			$this->session->clearArray('basket');
			include_once("core/front/FRONT.php");
			$obj = new FRONT($this->db);
			$obj->message = $this->success->text("basketDelete");
			return $obj->display();
		}
// Ask for confirmation first
		include_once("core/template/TEMPLATE.php");
		$template = new TEMPLATE('content');
		$template->setVar('heading', $this->messages->text("heading", "basketDelete"));
		include_once("core/html/FORM.php");
		$pString = FORM::formHeader("basketDeleteConfirm");
		$pString .= FORM::formSubmit('Confirm') . FORM::formEnd();
		$template->setVar('body', $pString);
		return $template->process();
	}
}
?>